package com.mzy.linear_struct.面试150题;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: mizy3
 * @Date: 2024/08/16/下午3:03
 * @Description:
 */
public class Pro242有效的字母异位词 {
    Map<Character, Integer> map = new HashMap<>();

    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) return false;

        char[] sarr = s.toCharArray();
        char[] tarr = t.toCharArray();
        for (int i = 0; i < s.length(); i++) {
            map.put(sarr[i], map.getOrDefault(sarr[i], 0) + 1);
            map.put(tarr[i],map.getOrDefault(tarr[i], 0) - 1 );
        }

        for (Integer value : map.values()) {
            if(value != 0){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        new Pro242有效的字母异位词().isAnagram("anagram"
        , "nagaram"
        );
    }
}
